package com.share.beta.factory.v2;

import com.share.beta.factory.PayService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 通过ApplicationContextAware 方式注入
 */
@Component
public class PayFactoryV2 implements ApplicationContextAware {

    private Map<String, PayService> map = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext.getBeansOfType(PayService.class).forEach((k, v) -> map.put(v.getCode(), v));
    }

    public PayService getService(String code) {
        return map.get(code);
    }
}
